﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Ivy.UserRegistration.Entity;
using Ivy.UserRegistration.IBusinessLogic;
using Ivy.UserRegistration.BusinessLogic;

using Ivy.Common.Framework.GeneralUtility;

using DasIdeen.ViewEntity;

namespace DasIdeen.UserControls
{
    public partial class VerificationControl : System.Web.UI.UserControl
    {
        #region  Variable & constant
        #endregion  Variable & constant

        #region Constructor & Destructor
        #endregion Constructor & Destructor

        #region Properties
        #endregion Properties

        #region Public Methods
        #endregion Public Methods

        #region Events
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserContext"] == null)
            {
                // Response.Redirect("~/Default.aspx");
            }
            else
            {
                if (!IsPostBack)
                {
                    UserContext UserSession = new UserContext();
                    UserSession = (UserContext)Session["UserContext"];
                    if (UserSession.CurrentUser.UserIsVerified == true)
                    {
                        Response.Redirect("../Default.aspx");
                    }
                    else
                    {
                        lblEmailId.ForeColor = System.Drawing.Color.Blue;
                        lblEmailId.Text = UserSession.CurrentUser.UserPrimaryEmailID;
                        lblUserName.ForeColor = System.Drawing.Color.Blue;
                        lblUserName.Text = UserSession.CurrentUser.UserLoginID;
                    }
                }

            }
        }

        protected void btnConfirm_Click(object sender, EventArgs e)
        {
            try
            {
                UserContext tempUserSession = new UserContext();
                tempUserSession = (UserContext)Session["UserContext"];
                IUserRegistrationBus lUserRegistrationBus = new UserRegistrationBus();
                User lUser = new User();
                lUser = lUserRegistrationBus.GetUserByLoginID(tempUserSession.CurrentUser.UserLoginID);
                if (lUser.UserVerificationCode == txtVerify.Text.Trim())
                {
                    lUser.UserIsVerified = true;
                    if (lUserRegistrationBus.UpdateRegisteredUser(lUser))
                    {
                        tempUserSession.CurrentUser = lUser;
                        Session["UserContext"] = tempUserSession;
                        Response.Redirect("~/Default.aspx");
                        //Response.Redirect(Request.UrlReferrer.ToString());
                    }
                }
                else
                {
                    lblInCorrect.ForeColor = System.Drawing.Color.Red;
                    lblInCorrect.Text = " Invailed Code ";
                }

            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
                //Need to display the exception message to the user or log it in application log.
            }
        }

        protected void btnResend_Click(object sender, EventArgs e)
        {
            try
            {
                IUserRegistrationBus lUserRegistrationBus = new UserRegistrationBus();
                User lUser = new User();
                UserContext UserSession = new UserContext();
                UserSession = (UserContext)Session["UserContext"];
                lUser = lUserRegistrationBus.GetUserByLoginID(UserSession.CurrentUser.UserLoginID);
                if (lUserRegistrationBus.SendVerificationCodeEmail(lUser))
                {
                    lblResendMsg.Text = "Verification Code Sent to your Primary Mail Id.";
                }
                else
                {
                    lblResendMsg.ForeColor = System.Drawing.Color.Red;
                    lblResendMsg.Text = "Error in sending";
                }

            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
                //Need to display the exception message to the user or log it in application log.
            }
        }

        protected void btnNotNow_Click(object sender, EventArgs e)
        {
            try
            {
                UserContext tempUserSession = new UserContext();
                tempUserSession = (UserContext)Session["UserContext"];
                tempUserSession.IsUserVerificationRequired = false;
                Session["UserContext"] = tempUserSession;
                Response.Redirect("~/Default.aspx");
            }
            catch (Exception ex)
            {
                ExceptionUtility.ExceptionHandler(ex, Server.MapPath("~/"));
            }
        }
        #endregion Events

        #region Private Methods
        private void CheckUserAutherization()
        {
        }

        private void ClearScreen()
        {
        }

        private void InitializeScreen()
        {
            ClearScreen();
            FillLoadLookUpData();
        }

        private void FillLoadLookUpData()
        {
        }

        private void TranslateEntityToUIControls()
        {
        }

        private void TranslateUIControlsToEntity()
        {
        }
        #endregion Private Methods

       
    }
}